﻿{% extends "layout/layouthead.html" %}

{% block content %}
{% load staticfiles %}
<script src="{% static 'app/scripts/Base/jquery.easyui.min.js' %}"></script>
<script src="{% static 'app/scripts/PageHelper/iframeTools.source.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/content/newSystem.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'app/styles/controls/tree.css' %}">
<div id="app">

<div class="h2tit">
    <span class="r">
            <a href="javascript:;" name="btnAddDept" class="add-department" onclick="add();"><i class="icon-added"></i>新增部门</a>
                    <a href="javascript:;" onclick="estateDataFind();">楼盘数据反查</a>
                    <a href="javascript:;" class="refreshData">刷新部门权限设置</a>
                     <a href="javascript:;" onclick="transferDepartment();">整组异动</a>       
        &nbsp;
    </span>
    <h2><i></i>组织结构</h2>
</div>
<div class="clearfix">
    <div class="role-left l">
        <div class="tree-head clearfix">
            <span class="r h26lh26">
                <a href="javascript:void(0)" onclick="javascript:$('#tt').tree('expandAll');">展开</a>
                <a href="javascript:void(0)" onclick="javascript:$('#tt').tree('collapseAll');">折叠</a>
            </span>
            <input type="text" id="txtDeptName" class="ipt w124 ac_input" value="" onkeydown="enterPress(event)" autocomplete="off">
            <input type="hidden" id="hidSearchDeptId" value="">
            <input type="hidden" id="hidDeptId" value="">
            <input type="hidden" id="hidAreKeytIds" value="">
            <input type="hidden" id="hidEstateKeytIds" value="">
            <input type="hidden" id="hidEstateCheckNum" value="">
            <input type="hidden" id="hidAreCheckNum" value="">
            <input type="button" id="btnSearch" class="btn-red" value="搜索">
        </div>
        <div class="easyui-panel-wrapper">
            <img src="/static/app/img/loading.gif" id="centerLoading" style="display: none;">
            
            <div class="panel" style="display: block;">
			<div class="easyui-panel mt10 panel-body panel-body-noheader" title="" style="width: 288px;">
                <ul id="tt" class="easyui-tree tree tree-lines" data-options=""><li>
					<div id="_easyui_tree_1" class="tree-node tree-root-one tree-node-last">
	<span class="tree-hit tree-expanded">
	</span>
	<span class="tree-icon tree-folder tree-folder-open ">
	</span>
	<span class="tree-title">[99]系统管理组
	</span>
</div>
</li>
</ul>
<ul style="display:block">
<li>
<div id="_easyui_tree_2" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9902]支持组
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_3" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9903]运维组
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_4" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9904]测试部门C3
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_5" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-hit tree-expanded">
	</span>
	<span class="tree-icon tree-folder tree-folder-open ">
	</span>
	<span class="tree-title">[9905]敏捷测试
	</span>
</div>
<ul style="display:block">
<li>
<div id="_easyui_tree_6" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-hit tree-expanded">
	</span>
	<span class="tree-icon tree-folder tree-folder-open ">
	</span>
	<span class="tree-title">[990501]测试部门01(删除)
	</span>
</div>
<ul style="display:block">
<li>
<div id="_easyui_tree_7" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-hit tree-expanded">
	</span>
	<span class="tree-icon tree-folder tree-folder-open ">
	</span>
	<span class="tree-title">[99050101]测试部门02(删除)
	</span>
</div>
<ul style="display:block">
<li>
<div id="_easyui_tree_8" class="tree-node tree-node-last">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-joinbottom">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9905010104]测试部门05(删除)
	</span>
</div>
</li>
</ul>
</li>
<li>
<div id="_easyui_tree_9" class="tree-node tree-node-last">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-joinbottom">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[99050102]测试部门03(删除)
	</span>
</div>
</li>
</ul>
</li>
<li>
<div id="_easyui_tree_10" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-hit tree-expanded">
	</span>
	<span class="tree-icon tree-folder tree-folder-open ">
	</span>
	<span class="tree-title">[990503]敏捷测试123
	</span>
</div>
<ul style="display:block">
<li>
<div id="_easyui_tree_11" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[99050301]测试
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_12" class="tree-node tree-node-last">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-joinbottom">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[99050302]部门资源cs
	</span>
</div>
</li>
</ul>
</li>
<li>
<div id="_easyui_tree_13" class="tree-node tree-node-last">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-line">
	</span>
	<span class="tree-indent tree-joinbottom">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[990504]敏捷测试123
	</span>
</div>
</li>
</ul>
</li>
<li>
<div id="_easyui_tree_14" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9907]部门资源c2s
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_15" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9908]部门资源c1d
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_16" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9909]测试csf部门
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_17" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9910]测试csf1部门
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_18" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9911]部门资源cs(删除)
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_19" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9912]测试123q
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_20" class="tree-node">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-join">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9913]测试12wq
	</span>
</div>
</li>
<li>
<div id="_easyui_tree_21" class="tree-node tree-node-last">
	<span class="tree-indent">
	</span>
	<span class="tree-indent tree-joinbottom">
	</span>
	<span class="tree-icon tree-file ">
	</span>
	<span class="tree-title">[9914]测试12111
	</span>
</div>
</li>
</ul>
</li>
</ul>
            </div></div>
        </div>
    </div>
    <div class="role-right">
        <!--列表 start-->
        <table id="grdDepartment" class="data-table w-all">
            <thead>
                <tr>
                    <th>部门编号</th>
                    <th>部门名称</th>
                    <th>部门负责人</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <!--列表 end-->
        <div class="mt10" id="tabOrg" style="display: none;">
            <div class="tab-hd">
                <ul class="tab-nav" id="titleid"></ul>
            </div>
            <div class="tab-bd p10" id="contentdiv"></div>
        </div>
    </div>
</div>

<!--绑定部门的权限-->
<div style="display: none">
    <!--绑定部门的删除和编辑权限-->

    <input type="hidden" value="True" id="hideditOrg">
    <input type="hidden" value="True" id="hiddeleteOrg">

    <!--绑定部门的设置数据范围权限-->
    <input type="hidden" value="True" id="hidDeptDataAdd">
    <input type="hidden" value="True" id="hidDeptDatagSearch">
    <input type="hidden" value="True" id="hidDeptDataDelete">
    <input type="hidden" value="True" id="hidDeptDataQuery">

    <!--绑定部门的部门设置权限-->
    <input type="hidden" value="True" id="hidDeptSet">
</div>
<script>
    function getURL() {
        var url = '/org/get-department-tree/' + Math.random();
        return url;
    }
    function detailTab() {
        var editTabMenu = $(".tab-nav li");
        var editTabCont = $(".tab-bd .tab-panel");
        editTabMenu.each(function () {
            $(this).click(function () {
                var index = editTabMenu.index(this);
                $(this).addClass("current").siblings().removeClass("current");
                editTabCont.eq(index).css("display", "block").siblings().css("display", "none");
            });
        });
    };

    var org = {};
    org.init = function () {
        var option = {
            titleID: "titleid",
            contentID: "contentdiv",
            currentClassName: "current",
            titleFun: org.getTitles,
            urlFun: org.getTabUrl,
            dataFun: org.getData,
            isIframe: true,
            contentFun: null
        };
        tabHelper(option);
    }

    detailTab();
    org.getTitles = function () {
        if ($("#hidDeptDataAdd").val() == "True" || $("#hidDeptDatagSearch").val() == "True"
            || $("#hidDeptDataDelete").val() == "True") {
            if ($("#hidDeptSet").val() == "True") {

                return ["通盘房源", "员工列表", "部门设置"];
            }
            else {
                return ["通盘房源", "员工列表"];
            }
        }
        else {

            return ["员工列表"];
        }
    }
    var deptId = $("#hidDeptId").val();
    org.getTabUrl = function (tabname) {
        var urlNum = window.location.href.split('=')[1];
        if (tabname == "通盘房源") {
            return "/org/data-scope?deptId=" + deptId;
        }
        if (tabname == "员工列表") {
            return "/org/org-employee-list?deptId=" + deptId;
        }
        if (tabname == "部门设置") {
            return "/org/org-department-settings?deptId=" + deptId;
        }
    };
    org.getData = function (tabname) {
        return [{ name: "key", value: tabname }];
    }
    var select = function (n) {
        deptId = n.id;
        $("#hidDeptId").val(n.id);
        bindDepartmentGrid(n.id);
    }

    var bindDepartmentGrid = function (departmentId) {
        if (departmentId == "") {
            $("#grdDepartment tbody tr").remove();
        }
        else {
            var root = $("#tt").tree("getRoot");
            if (root.id == departmentId && root.text == "[szzy]深圳中原") {
                $("#grdDepartment tbody tr").remove();
                $("#tabOrg").hide();
                // $("#hidDeptId").val("");
                return;
            }

            $("#tabOrg").show();
            org.init();

            $.ajax({
                url: "/org/get-department/?keyId=" + departmentId,
                cache: false,
                success: function (d) {
                    var rowHtml = "<tr>"
                        + "  <td>" + d.DepartmentNo + "</td>"
                        + "  <td>" + d.DepartmentName + "</td>"
                        + "  <td>" + null2String(d.DeptChief) + "</td>"
                        + "  <td>" + formatDate(d.CreateTime) + "</td>"
                        + "  <td>"
                    if ($("#hideditOrg").val() == "True" ) {
                        rowHtml = rowHtml + "    <a href=\"javascript:update();\" class=\"edit-department\">编辑</a>"
                    }
                    if ($("#hiddeleteOrg").val() == "True"  && d.DeptCCHRKeyId==null) {
                        rowHtml = rowHtml + "    <a href=\"javascript:remove();\" class=\"remove-dept\">删除</a>"
                    }
                    rowHtml = rowHtml + "  </td>"
                    rowHtml = rowHtml + "</tr>";
                    $("#grdDepartment tbody tr").remove();
                    $("#grdDepartment tbody").append(rowHtml);
                },
                error: function (e) { }
            });
        }

        function null2String(value) {
            if (value == null) {
                return "";
            }
            else {
                return value
            }
        }
        function formatDate(value) {
            var date = new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
            return date.getFullYear() + "/" + (parseInt(date.getMonth()) + 1) + "/" + date.getDate();
        }
    }

    var estateDataFind = function () {
        var url = " /org/estate-data-find?searchType=estate";
        var dialogDataFindEstate = art.dialog.open(url, {
            id: "dialogDataFindEstate",
            title: '楼盘数据反查',
            width: 700,
            lock: true,
            cache: false
        });

    };


    var add = function () {
        var parentDeptId = $("#hidDeptId").val();
        if (parentDeptId == "") {
            $.OKDialogAgency('<div class="tc">请选择部门！</div>');
            return;
        }
        var url = "/org/add-department/?keyId=" + $("#hidDeptId").val() + "&rootId=" + $("#tt").tree("getRoot").id;
        var dialogAddDept = art.dialog.open(url, {
            id: "dialogAddDept",
            title: '新增部门',
            lock: true,
            cache: false
        });
    }

    var update = function () {
        var url = "/org/edit-org-dept/?keyId=" + $("#hidDeptId").val();
        var dialogEditDept = art.dialog.open(url, {
            id: "dialogEditDept",
            title: '编辑部门',
            lock: true,
            cache: false
        });
    }
    //整租异动
    var transferDepartment = function () {
        var url = "/org/transfer-department";
        var dialogEditDept = art.dialog.open(url, {
            id: "dialogtransferDept",
            title: '整组异动',
            width: 500,
            lock: true,
            cache: false
        });
    }
    var remove = function (keyId) {

        $.OKCancelDialogAgency('<div class="tc">确定要删除部门吗?</div>', '删除部门', true, function () {
            $.ajax({
                url: "/org/remove-dept/?deptId=" + $("#hidDeptId").val(),
                method: "post",
                success: function (d) {
                    if (d == "OK") {
                        $.OKDialogAgency('<div class="tc">操作成功！</div>', '提示', function () {
                            $(this).parent().prev().remove();
                            window.location.reload();
                        });
                    }
                    else {
                        $.OKDialogAgency('<div class="tc">' + d + '</div>');
                    }
                }
            });
        }, null);
    }


    //$("#txtDeptName").autocomplete(function () {
    //    var url = String.format("/org/auto-match-department?deptName={0}&v={1}", escape($("#txtDeptName").val()), Math.random());
    //    return url;
    //}, {
    //    width: 260,
    //    max: 12,
    //    selectFirst: false,
    //    formatItem: function (item) {
    //        return String.format("[{0}]{1}", item.DepartmentNo, item.DepartmentName);
    //    },
    //    formatResult: function (item) {
    //        return String.format("{0}", item.DepartmentName);
    //    }
    //}).result(function (event, item) {
    //    $("#hidSearchDeptId").val(item.DepartmentKeyId);
    //});

    var openDialog = function (title, popUrl, data) {
        art.dialog.open(popUrl, {
            id: "openDialog",
            title: title,
            lock: true,
            width: 700
        });
        art.dialog.data("data", data);
    }

    //数据权限对话框
    var openPerDialog = function (url, returnUrl, obj) {
        art.dialog.open(url, {
            id: "openPerDialog",
            title: '设置数据权限',
            lock: true,
            width: 630,
            cache: false
        });
        art.dialog.data("returnUrl", returnUrl);
    }

    //获取子页面传过来的片区ID值
    var getAreKeyIds = function (arekeyIds) {
        // alert(areKeyIds);
        $("#hidAreKeytIds").val(arekeyIds);
    }

    //获取子页面传过来的者楼盘的ID值
    var getEstateKeyIds = function (estatekeyIds) {
        $("#hidEstateKeytIds").val(estatekeyIds);
    }


    function treeReload(type, keyId) {

        //整租异动後刷新
        if (type == "transfer") {
            try {
                art.dialog({ id: 'dialogtransferDept' }).close();
            } catch (e) {
                //IE下關不了，吃掉這個錯誤
            }
            var dd = $("#tt").tree("reload");
        }
        //新增后刷新
        if (type == "add") {

            try {
                art.dialog({ id: 'dialogAddDept' }).close();
            } catch (e) {
                //IE下关不了，吃掉这个错误
            }
            var ss = $("#tt").tree("reload");
            $("#grdDepartment tbody tr").remove();
            $("#tabOrg").hide();
            $("#hidDeptId").val("");
            setTimeout(function () {
                var node = $("#tt").tree('find', keyId);
                if (node != null) {
                    $('#tt').tree('select', node.target);
                    $(node.target).trigger("click");
                    $('#tt').tree('scrollTo', node.target);
                    bindDepartmentGrid(keyId);
                    //过滤取消的部门
                    fillterDepatment();
                }
            }, 1000);
        }
        //修改后刷新
        if (type == "edit") {
            try {
                art.dialog({ id: 'dialogEditDept' }).close();
            } catch (e) {
                //IE下关不了，吃掉这个错误
            }
            // $("#tt").tree("reload");
            var deptId = $("#hidDeptId").val();
            var node = $("#tt").tree('find', deptId);
            if (node != null) {
                bindDepartmentGrid(deptId);
            }
        }
    }

    function enterPress(e) {
        var e = e || window.event;
        if (e.keyCode == 13) {
            $("#btnSearch").click();
        }
    }

    //过滤取消的部门
    function fillterDepatment() {
        $("#tt").find(".tree-title").each(function () {
            if ($(this).text().indexOf("取消") > 0) {
                $(this).parent().css("display", "none");
            }
        });
        $("#radioFillter #radioReset").attr("checked", "true")
    }

    $(function () {
        $(".refreshData").click(function () {
            $.AjaxAgency("/Org/refresh-DeptDataPermisstion", null, function (data) {
                if (data == "OK") {
                    $.OKDialogAgency('<div class="tc">任务已提交,系统后台正在处理！</div>');
                }
                else {
                    $.OKDialogAgency('<div class="tc">' + data + '</div>');
                }
            });
        });

        $("#btnSearch").bind("click", function () {
            var deptId = $("#hidSearchDeptId").val();
            var node = $("#tt").tree('find', deptId);
            if (node != null) {
                //同时展开树
                $('#tt').tree('expandAll');
                setTimeout(function () {
                    $('#tt').tree('select', node.target);
                    $(node.target).trigger("click");
                    $('#tt').tree('scrollTo', node.target);
                }, 1000);

            }
        });
        //过滤取消的部门
        $("#radioFillter #radioReset").bind("click", function () {
            fillterDepatment();
        });

        //显示所有部门
        $("#radioFillter #radioAll").bind("click", function () {
            $("#tt").find(".tree-title").each(function () {
                if ($(this).text().indexOf("取消") > 0) {
                    $(this).parent().css("display", "block");
                }
            });
        });
    })
</script>
</div>
{% endblock %}


{% block scripts %}

    {% load staticfiles %}
{% endblock %}
